//页面加载需要做的事情

$(function () {
    $("#username").on('keyup',function () {
        checkUserName();
    }),
        $(".button > div:first").on('click',function () {
        register();
    }),
        $(".button > div:nth-child(2)").on('click',function () {
            window.open("login.html","_self")
        }),
        $("#repeat-password").on('keyup',function () {
            checkPassword();
        }),
        $("#phone").on('keyup',function () {
            checkPhone();
        }),
        $("#email").on('keyup',function () {
        checkEmail();
    })
})

//抽取的判断函数
function myReg(reg,value,obj,success,error) {
    if (reg.test(value)) {
        $(obj).parent().next().html(success);
        $(obj).parent().next().removeClass();
        $(obj).parent().next().addClass("success-tip");
        return true;
    }
    else {
        $(obj).parent().next().html(error);
        $(obj).parent().next().removeClass();
        $(obj).parent().next().addClass("error-tip");
        return false;
    }
}


//检查用户名
function checkUserName() {
  // let  username=document.getElementById("username");
  // let value=username.value;
  //  console.log(value);
// 忽略单双引"}
 let username= $("#username").val();
 let reg=/^[a-zA-Z]\w{5,15}$/;
 return myReg(reg,username,$("#username"),"验证通过","用户名格式错误");
}

//检查密码
function  checkPassword() {
    let password= $("#password").val();
    let  repeat_password=$("#repeat-password").val();
    let obj=$("#repeat-password");
    if(password!=repeat_password){
        $(obj).parent().next().html("前后密码不一致");
        $(obj).parent().next().addClass("error-tip");
        return false;
    }
    $(obj).parent().next().html("");
    $(obj).parent().next().removeClass();
    return  true;
    
}

//检查电话
//
function checkPhone(){
    let  phone=$("#phone").val();
    let reg=/^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{8}$/;
    return myReg(reg,phone,$("#phone"),"验证通过","电话号码格式错误")
}
//检查邮箱
function checkEmail(){
    let  email=$("#email").val();
    let reg=/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    return myReg(reg,email,$("#email"),"验证通过","邮箱格式错误")
}




function register() {
    if(checkUserName()&&checkPassword()&&checkEmail()&&checkPhone()){
        let  username=$("#username").val();
        let  password=$("#password").val();
        let  phone=$("#phone").val();
        let  sex=$("input[type=radio]:checked").val();
        let  hobby=getHobby().toLocaleString();
        let  birthday=$("#date").val();
        let  email=$("#email").val();
        let obj={username:username,password:password,phone:phone,sex:sex,hobby:hobby,birthday:birthday,email:email};
       $.ajax({
            url:'/register',//对应的是webServlet的注解路径
            data:obj,//
            type:'post',
            dataType:'json',
            success:function (data) {
                if(data.result=="success")
                 alert("注册成功");
                 alert("注册失败")
            },


        })

    }

}


function getHobby() {
    let vals=[];
    // $("input[type=checkbox][name=hobby]:checked").each(function (index,item) {
    //     vals.push($(this).val());
    // });
    //
    $.each($("input[type=checkbox]:checked"),function () {
        vals.push($(this).val());
    })

    return vals;
}

